<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.text.DateFormat"%>
<%@page import="com.zenika.supbook.model.User"%>
<%@page import="com.zenika.supbook.service.UserService"%>
<%
try {
	DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
	User user = new User();
	user.setLogin(request.getParameter("login"));
	user.setFirstName(request.getParameter("firstName"));
	user.setLastName(request.getParameter("lastName"));
	user.setEmail(request.getParameter("email"));
	
	String birthdateParam = request.getParameter("birthDate");
	user.setBirthdate(dateFormat.parse(birthdateParam));
	
	String password = request.getParameter("password");
	String passwordConfirmation = request.getParameter("passwordConfirmation");
	if(password.equals(passwordConfirmation)) {		
		user.setPassword(password);
		new UserService().create(user);
		response.sendRedirect(request.getContextPath() + "/public/loginForm.jsp");
	} else {
		response.sendRedirect(request.getContextPath() + "/public/user/createUserForm.jsp?error_login=1");
	}
} catch(Exception e) {
	e.printStackTrace();
	response.sendRedirect(request.getContextPath() + "/public/user/createUserForm.jsp?error=1");
}
	
%>